package com.example.content.fragment.search

import androidx.recyclerview.widget.LinearLayoutManager
import com.example.content.*
import com.example.content.adapter.recycle.FunAction
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.entity.HomeItemEntity
import com.example.content.databinding.FragmentSearchProductLayoutBinding
import com.example.content.fragment.BaseMvvmFragment
import com.example.content.fragment.common.CommonListBindListener
import com.example.content.manager.GlobalMgr
import com.example.content.repository.bean.FollowReq
import com.example.content.repository.bean.ProductInfo
import com.example.content.repository.bean.UserActionReq
import com.example.content.viewmodule.search.SearchViewModel
import com.example.core.utils.DensityUtils

/**
 * Created by zhudongao
 * Desc
 * Date 5/12/21
 */
class SearchProductFragment :
    BaseMvvmFragment<FragmentSearchProductLayoutBinding, SearchViewModel>() {

    lateinit var recyclerAdapter: RecyclerAdapter<ProductInfo>

    override fun initView() {
        recyclerAdapter = RecyclerAdapter(CommonListBindListener(this))
        recyclerAdapter.addItemViewByType(PHOTO_LIST_TYPE, R.layout.item_photo_layout)
        recyclerAdapter.addItemViewByType(VIDEO_TYPE, R.layout.item_video_layout)
        dataBinding.searchRecycler.adapter = recyclerAdapter
        dataBinding.searchRecycler.layoutManager = LinearLayoutManager(context)
        dataBinding.searchRecycler.setPadding(
            DensityUtils.dp2px(10f),
            0,
            DensityUtils.dp2px(10f),
            0
        )
        recyclerAdapter.setEmptyView(R.layout.empty_pager_layout)
    }

    override fun initListener() {
        viewModel.productResult.observe(this, observer = {
            if (it.isEmpty()) {
                recyclerAdapter.updateData(emptyList())
            } else {
                recyclerAdapter.data.clear()
                for (recommend in it) {
                    val data: HomeItemEntity = when (recommend.resource_type) {
                        RESOURCE_TYPE_IMG -> {
                            HomeItemEntity(PHOTO_LIST_TYPE, recommend)
                        }
                        RESOURCE_TYPE_VIDEO -> {
                            HomeItemEntity(VIDEO_TYPE, recommend)
                        }
                        else -> {
                            HomeItemEntity(PHOTO_LIST_TYPE, recommend)
                        }
                    }
                    data.funAction = object : FunAction {
                        override fun follow(followReq: FollowReq) {
                            viewModel.follow(followReq)
                        }

                        override fun userAction(userActionReq: UserActionReq) {
                            viewModel.userAction(userActionReq)
                        }
                    }
                    recyclerAdapter.addData(data)
                }
                recyclerAdapter.notifyDataSetChanged()
            }
        })

        GlobalMgr.searchKey.observe(viewLifecycleOwner, {
            if (it != null) {
                viewModel.search(it, SEARCH_PAGE_PRODUCT)
            }
        })
    }

    override fun getViewModuleClass(): Class<SearchViewModel> {
        return SearchViewModel::class.java
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_search_product_layout
    }
}